package pw.hais.main.main_app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.squareup.okhttp.Response;

import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import pw.hais.http.base.OnHttpListener;
import pw.hais.main.R;
import pw.hais.main.main_app.adapter.HaiHanModelAdapter;
import pw.hais.main.main_app.app.BaseActivity;
import pw.hais.main.main_app.dao.NeiHan8Dao;
import pw.hais.main.main_app.enums.HaiHanType;
import pw.hais.main.main_app.model.NeiHan8ArticleModel;
import pw.hais.utils.L;

/**
 * 海涵，内涵图片、段子
 * Created by Hais1992 on 2015/12/16.
 */
public class HaiHanActivity extends BaseActivity implements AdapterView.OnItemClickListener, PtrHandler, AbsListView.OnScrollListener {
    private ListView listview_haihan;
    private PtrClassicFrameLayout store_house_ptr_frame;
    private HaiHanModelAdapter haiHanModelAdapter;
    private int page = 1;
    private HaiHanType type = HaiHanType.Images;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_haihan);

        listview_haihan = (ListView) findViewById(R.id.listview_haihan);
        listview_haihan.setOnItemClickListener(this);
        store_house_ptr_frame = (PtrClassicFrameLayout) findViewById(R.id.store_house_ptr_frame);
        store_house_ptr_frame.setPtrHandler(this);
        store_house_ptr_frame.autoRefresh();

        View headerView = LayoutInflater.from(context).inflate(R.layout.activity_haihan_list_header, null);
        View footerView = LayoutInflater.from(context).inflate(R.layout.activity_haihan_list_footer, null);
        listview_haihan.addHeaderView(headerView);
        listview_haihan.addFooterView(footerView);
        listview_haihan.setOnScrollListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position == 0)return;
        Intent intent = new Intent(context, HaiHanDetailsActivity.class);
        intent.putExtra(HaiHanDetailsActivity.INTENT_CONTENT_Id_KEY, haiHanModelAdapter.getItem(position-1).id);
        intent.putExtra(HaiHanDetailsActivity.INTENT_CONTENT_TYPE_KEY,type.string);
        startActivity(intent);
    }


    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, listview_haihan, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        page = 1;
        getHttpDataAndShow(page + "");
    }


    private void getHttpDataAndShow(final String page) {
        NeiHan8Dao.getList(type, page, new OnHttpListener<List<NeiHan8ArticleModel>>() {
            @Override
            public void onSuccess(Response response, List<NeiHan8ArticleModel> data) {
                if (data == null) {
                    L.showShort("没有内容！");
                    return;
                }
                if ("1".equals(page)) {
                    if (haiHanModelAdapter == null) {
                        haiHanModelAdapter = new HaiHanModelAdapter(data, HaiHanModelAdapter.ViewHolder.class);
                        listview_haihan.setAdapter(haiHanModelAdapter);
                    } else {
                        haiHanModelAdapter.removeListData();
                    }
                }
                haiHanModelAdapter.appendData(data);
                store_house_ptr_frame.refreshComplete();
            }
        });
    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            // 当不滚动时
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                // 判断滚动到底部
                if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
                    page = page + 1;
                    getHttpDataAndShow(page + "");
                }
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_haihan, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_image_article) {
            switch (type) {
                case Article:
                    type = HaiHanType.Images;
                    L.showShort("已切换到 图片");
                    onRefreshBegin(store_house_ptr_frame);
                    break;
                case Images:
                    type = HaiHanType.Article;
                    L.showShort("已切换到 文章");
                    onRefreshBegin(store_house_ptr_frame);
                    break;
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
